package com.arajuuri.catinvaders;

import java.awt.Graphics2D;
import java.awt.Image;
import java.util.Iterator;
import java.util.Vector;

/**
 * <p>Title: Cat Invaders</p>
 * <p>Description: A space invaders clone with cats</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author Semi Arajuuri
 * @version 1.0
 */

public class CatLine extends GraphicalObjectAdapter {
  Vector cats=new Vector();
  int vSpace=30;
  int catLineWidth=0;

  public CatLine() {
  }

  public CatLine(int y, int speed, int gravity){
    this.y=y;
    this.startY=y;
    this.speed=speed;
    this.gravity=gravity;
  }

  public void addCat(Cat cat){
    cats.add(cat);
    catLineWidth+=cat.getImage().getWidth(null);
    if(catLineWidth!=0){
      catLineWidth+=vSpace;
    }
    x=400-catLineWidth/2;
    startX=x;
  }

  public void draw(Graphics2D g2d){
    hidden=true;
    Iterator catIterator=cats.iterator();
    while(catIterator.hasNext()){
      Cat cat=(Cat)catIterator.next();
      if(!cat.isHidden()){
        g2d.drawImage(cat.getImage(), cat.getX(), cat.getY(), null);
        hidden=false;
      }
    }
  }

  public int getHeight(){
    if(cats.size()>0){
      return ( (Cat) cats.get(0)).getHeight();
    }
    else{
      return 0;
    }
  }
  public void move(){
    int next=0;
    x+=speed;
    if(x>800-catLineWidth || x<0){
      y+=gravity;
      speed=speed*-1;
    }
    Iterator catIterator=cats.iterator();
    while(catIterator.hasNext()){
      Cat cat=(Cat)catIterator.next();
      Image img=cat.getImage();
      cat.setCoordinates(x + next, y);
      next += img.getWidth(null) + vSpace;
    }
  }

  public boolean detectCollision(int x, int y){
    Iterator catIterator=cats.iterator();
    while(catIterator.hasNext()){
      Cat cat=(Cat)catIterator.next();
      if(cat.detectCollision(x, y)){
        return true;
      }
    }
    return false;
  }
}
